<template>

  <dev>
    <h1>立项管理</h1>
    <dev>
      <!--          搜索区域-->
      <el-form :model="queryParams" ref="queryForm" size="small" :inline="true" v-show="showSearch" label-width="68px">
        <el-form-item label="关键词" prop="keyword">
          <el-input
            v-model="queryParams.keyword"
            placeholder="请输入项目名称、合同号或负责人关键词"
            clearable
            @keyup.enter.native="handleQuery"
            style="width: 400px;"
          />
        </el-form-item>
        <el-form-item>
          <el-button type="primary" icon="el-icon-search" size="mini" @click="handleQuery">搜索</el-button>
          <el-button icon="el-icon-refresh" size="mini" @click="resetQuery">重置</el-button>
        </el-form-item>
      </el-form>


      <!--  按钮区域-->


      <!-- 右侧导入/导出按钮 -->
      <el-row :gutter="10" class="mb8" style="margin-bottom: 10px;">
        <el-col :span="24" style="text-align: right;">
          <el-button
            type="warning"
            plain
            icon="el-icon-download"
            size="mini"
            @click="handleExport"
            v-hasPermi="['pm:project:export']"
          >导出</el-button>
          <el-button
            type="info"
            plain
            icon="el-icon-upload"
            size="mini"
            @click="handleImport"
            v-hasPermi="['pm:project:import']"
          >导入</el-button>
        </el-col>
      </el-row>

      <!-- 导入对话框 -->
      <el-dialog :title="upload.title" :visible.sync="upload.open" width="400px" append-to-body>
        <el-upload ref="upload" :limit="1" accept=".xlsx, .xls" :headers="upload.headers" :action="upload.url + '?updateSupport=' + upload.updateSupport" :disabled="upload.isUploading" :on-progress="handleFileUploadProgress" :on-success="handleFileSuccess" :auto-upload="false" drag>
          <i class="el-icon-upload"></i>
          <div class="el-upload__text">将文件拖到此处，或<em>点击上传</em></div>
          <div class="el-upload__tip text-center" slot="tip">
            <div class="el-upload__tip" slot="tip">
              <el-checkbox v-model="upload.updateSupport" />是否更新已存在数据
            </div>
            <span>仅允许导入xls、xlsx格式文件。</span>
            <el-link type="primary" :underline="false" style="font-size: 12px; vertical-align: baseline" @click="importTemplate">下载模板</el-link>
          </div>
        </el-upload>
        <div slot="footer" class="dialog-footer">
          <el-button type="primary" @click="submitFileForm">确 定</el-button>
          <el-button @click="upload.open = false">取 消</el-button>
        </div>
      </el-dialog>
      <!-- 添加或修改公告对话框 -->
      <el-dialog :title="title" :visible.sync="open" width="780px" append-to-body>
        <el-form ref="form" :model="form" :rules="rules" label-width="80px">
          <el-row>
            <el-col :span="12">
              <el-form-item label="项目名称" prop="projectName">
                <el-input v-model="form.projectName" placeholder="请输入项目名称" />
              </el-form-item>
            </el-col>

            <el-col :span="12">
              <el-form-item label="合同编号" prop="projectNo">
                <el-input v-model="form.projectNo" placeholder="请输入合同编号" />
              </el-form-item>
            </el-col>

<!--            <el-col :span="12">-->
<!--              <el-form-item label="合同编号" prop="projectNO">-->
<!--                <el-input v-model="form.projectNo" placeholder="请输入合同编号" />-->
<!--              </el-form-item>-->
<!--            </el-col>-->

            <el-col :span="12">
              <el-form-item label="项目类型" prop="projectType">
                <el-select v-model="form.projectType" placeholder="请输入项目类型">
<!--                  <el-option-->
<!--                    v-for="dict in dict.type.sys_notice_type"-->
<!--                    :key="dict.value"-->
<!--                    :label="dict.label"-->
<!--                    :value="dict.value"-->
<!--                  ></el-option>-->
                </el-select>
              </el-form-item>
            </el-col>

<!--            <el-col :span="24">-->
<!--              <el-form-item label="状态">-->
<!--                <el-radio-group v-model="form.status">-->
<!--                  <el-radio-->
<!--                    v-for="dict in dict.type.status"-->
<!--                    :key="dict.value"-->
<!--                    :label="dict.value"-->
<!--                  >{{dict.label}}</el-radio>-->
<!--                </el-radio-group>-->
<!--              </el-form-item>-->
<!--            </el-col>-->

            <el-col :span="12">
              <el-form-item label="状态" prop="status">
                <el-input v-model="form.status" placeholder="请输入状态" />
              </el-form-item>
            </el-col>

            <el-col :span="12">
              <el-form-item label="负责人" prop="projectManager">
                <el-input v-model="form.projectManager" placeholder="请输入负责人" />
              </el-form-item>
            </el-col>

            <el-col :span="12">
              <el-form-item label="创建人ID" prop="createUserId">
                <el-input v-model="form.createUserId" placeholder="请输入创建人ID" />
              </el-form-item>
            </el-col>

            <el-col :span="12">
              <el-form-item label="创建人ID" prop="projectType">
                <el-input v-model="form.projectType" placeholder="请输入创建人ID" />
              </el-form-item>
            </el-col>

          </el-row>
        </el-form>
        <div slot="footer" class="dialog-footer">
          <el-button type="primary" @click="submitForm">确 定</el-button>
          <el-button @click="cancel">取 消</el-button>
        </div>
      </el-dialog>




      <el-row :gutter="10" class="mb8">
        <el-col :span="1.5">
          <el-button
            type="primary"
            plain
            icon="el-icon-plus"
            size="mini"
            @click="handleAdd"
            v-hasPermi="['pm:project:add']"
          >立项登记</el-button>
        </el-col>
<!--        <el-col :span="1.5">-->
<!--          <el-button-->
<!--              type="success"-->
<!--              plain-->
<!--              icon="el-icon-edit"-->
<!--              size="mini"-->
<!--              :disabled="single"-->
<!--              @click="handleUpdate"-->
<!--              v-hasPermi="['system:notice:edit']"-->
<!--          >修改</el-button>-->
<!--        </el-col>-->
        <right-toolbar :showSearch.sync="showSearch" @queryTable="getList"></right-toolbar>
      </el-row>



<!--      查询全部区域-->
            <el-table v-loading="loading" :data="projectList" @selection-change="handleSelectionChange">
        <el-table-column type="selection" width="55" align="center" />
        <el-table-column label="id" align="center" prop="id" width="100" />
<!--        <el-table-column-->
<!--          label="机会合同号"-->
<!--          align="center"-->
<!--          prop="projectNo"-->
<!--          :show-overflow-tooltip="true"-->
<!--        />-->
              <el-table-column label="机会合同号" align="center" prop="projectNo" width="200"/>
        <el-table-column label="项目名称" align="center" prop="projectName" width="200"/>
        <el-table-column label="项目状态" align="center" prop="status" width="100"/>
        <el-table-column label="负责人" align="center" prop="projectManager" width="100" />
        <el-table-column label="创建时间" align="center" prop="createTime" width="200"/>
        <el-table-column label="操作" align="center" class-name="small-padding fixed-width">
          <template slot-scope="scope">
            <el-button
              size="mini"
              type="text"
              icon="el-icon-edit"
              @click="handleUpdate(scope.row)"
              v-hasPermi="['system:notice:edit']"
            >查看</el-button>
          </template>
        </el-table-column>
      </el-table>

      分页
      <pagination
        v-show="total>0"
        :total="total"
        :page.sync="queryParams.pageNum"
        :limit.sync="queryParams.pageSize"
        @pagination="getList"
      />





    </dev>
  </dev>
</template>

<script>

import {listProject, getProject, addProject, updateProject} from "@/api/pm/projects";
import { getToken } from "@/utils/auth"

export default {
  name: "Projects",
  data() {
    return {
      // 遮罩层
      // loading: true,
      // 选中数组
      ids: [],
      // 非单个禁用
      single: true,
      // 非多个禁用
      multiple: true,
      // 显示搜索条件
      showSearch: true,
      // 总条数
      total: 0,
      // 弹出层标题
      title: "",
      // 公告表格数据
      projectList: [],

      // 查询参数
      queryParams: {
        id: undefined,
        pageNum: 1,
        pageSize: 10,
        keyword: undefined,
        status: undefined
      },
      // 表单参数
      form: {
      },
      // 表单校验
      rules: {
        projectName: [
          { required: true, message: "项目名称不能为空", trigger: "blur" }
        ],
        projectNo: [
          { required: true, message: "合同号不能为空", trigger: "change" }
        ]
      },
      upload: {
        open: false,
        title: "",
        isUploading: false,
        updateSupport: 0,
        headers: { Authorization: "Bearer " + getToken() },
        url: process.env.VUE_APP_BASE_API + "/pm/projects/importData"
      }
    }
  },

  created() {
    this.getList()
  },
  methods: {
    /** 导入按钮 */
    handleImport() {
      this.upload.title = "立项导入"
      this.upload.open = true
    },
    /** 下载模板 */
    importTemplate() {
      this.download('pm/projects/importTemplate', {}, `projects_template_${new Date().getTime()}.xlsx`)
    },
    // 文件上传中处理
    handleFileUploadProgress(event, file, fileList) {
      this.upload.isUploading = true
    },
    // 文件上传成功处理
    handleFileSuccess(response, file, fileList) {
      this.upload.open = false
      this.upload.isUploading = false
      this.$refs.upload.clearFiles()
      this.$alert("<div style='overflow: auto;overflow-x: hidden;max-height: 70vh;padding: 10px 20px 0;'>" + response.msg + "</div>", "导入结果", { dangerouslyUseHTMLString: true })
      this.getList()
    },
    // 提交上传文件
    submitFileForm() {
      this.$refs.upload.submit()
    },
    /** 导出按钮 */
    handleExport() {
      this.download('pm/projects/export', {
        ...this.queryParams
      }, `projects_${new Date().getTime()}.xlsx`)
    },
    /** 获取列表 */
    getList() {
      listProject(this.queryParams).then(res => {
        console.log(res)
        this.projectList = res.rows
        this.total = res.total
        this.loading = false
      })
    },

    /** 搜索按钮操作 */
    handleQuery() {
      this.queryParams.pageNum = 1
      this.getList()
    },

    /** 重置按钮操作 */
    resetQuery() {
      this.resetForm("queryForm")
      this.handleQuery()
    },

    /** 新增按钮操作 */
    handleAdd() {
      this.reset()
      this.open = true
      this.title = "立项登记"
    },

    // 取消按钮
    cancel() {
      this.open = false
      this.reset()
    },

    /** 修改按钮操作 */
    handleUpdate(row) {
      this.reset()
      const id = row.id || this.ids
      getProject(id).then(res => {
        this.form = res.data
        this.open = true
        this.title = "修改公告"
      })
    },

    // 表单重置
    reset() {
      this.form = {
        projectName: undefined,
        projectType: undefined,
        projectNo: undefined,
        projectManager: undefined,
        createUserId: undefined,
        createTime: undefined,
        updateTime: undefined,
        status: "未开始"
      }
      this.resetForm("form")
    },

    /** 提交按钮 */
    submitForm: function() {
      this.$refs["form"].validate(valid => {
        if (valid) {
          if (this.form.id != undefined) {
            updateProject(this.form).then(res => {
              this.$modal.msgSuccess("修改成功")
              this.open = false
              this.getList()
            })
          } else {
            addProject(this.form).then(res => {
              this.$modal.msgSuccess("新增成功")
              this.open = false
              this.getList()
            })
          }
        }
      })
    },

    // 多选框选中数据
    handleSelectionChange(selection) {
      this.ids = selection.map(item => item.id)
      this.single = selection.length!=1
      this.multiple = !selection.length
    },
  }
}

</script>

<style scoped lang="scss">

</style>
